package com.zy.socket.practies;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Author Gong
 * @Description 服务端读取图片并发送给客户端，客户端保存图片到本地
 * @dATE 2021/12/16 11:01
 **/
public class Client1 {
    public static void main(String[] args) {

        Socket socket =null;
        FileInputStream fileInputStream = null;
        try {
             socket = new Socket(InetAddress.getByName("127.0.0.1"),8989);


            //如果没有这句话，不能正常结束，服务器将一直阻塞
            socket.shutdownOutput();


            String filePath = "d:/temp/java_1x.png";
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
            InputStream inputStream = socket.getInputStream();
            byte[] b= new byte[1024];
            int len;
            while((len = inputStream.read(b)) != -1){
                fileOutputStream.write(b,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{

            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
